package com.zlq.iwork.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

public class XWord {
	
	private Context mContext;

	public static void main(String[] args) {
		List<MarkWithText> list = new ArrayList<MarkWithText>();

		list.add(new MarkWithText("[dept]", "软件部"));
		list.add(new MarkWithText("[name]", "臧路泉"));
		list.add(new MarkWithText("[ndate]", "2015-06-15"));
		list.add(new MarkWithText("[week]", "星期一"));
		list.add(new MarkWithText("[note]", "日志内容"));
		XWord xWord=new XWord();
		xWord.DoWordXmlAndSave(null,"note.xml", "note.doc", list);
	}

	/**
	 * 读取Word模板的XML文件，替换标签保存Word
	 * 
	 * @param sSourceDocUrl
	 *            模板地址
	 * @param sSaveDocUrl
	 *            保存地址
	 * @param list
	 *            标签对应文本集合
	 * @return
	 */
	public boolean DoWordXmlAndSave(Context context,String sSourceDocUrl, String sSaveDocUrl,
			List<MarkWithText> list) {
		mContext=context;
		return writeToDoc(replaceMarkToText(sSourceDocUrl, list),
				sSaveDocUrl);

	}

	/**
	 * 将word xml模板读取为字符串
	 * 
	 * @param sFileFullUrl
	 * @return
	 */
	private String ReadToString(String sFileFullUrl) {
		return FileUtils.readFile(sFileFullUrl, "utf-8").toString();

	}

	/**
	 * 替换标签
	 * 
	 * @param sSourceText
	 * @param list
	 * @return
	 */
	private String replaceMarkToText(String sSourceText, List<MarkWithText> list) {
		if (StringUtils.isEmpty(sSourceText)) {
			return "";
		}
		if (list.size() == 0) {
			return sSourceText;
		}
		String sReturnText = sSourceText;
		for (MarkWithText mt : list) {
			sReturnText = fixNewLine(sReturnText, mt.bookMark, mt.replaceValue);
		}
		return sReturnText;
	}

	/**
	 * 输出word文件
	 * 
	 * @param sSourceText
	 * @param sFileFullUrl
	 * @return
	 */
	private boolean writeToDoc(String sSourceText, String sFileFullUrl) {
		if (!sFileFullUrl.toUpperCase().endsWith(".DOC")) {
			sFileFullUrl = sFileFullUrl + ".doc";
		}
		try {
			ToolFile.write(mContext, sFileFullUrl, sSourceText);
			
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	/**
	 * 处理换行 "\r\n"
	 * 
	 * @param sSource
	 * @param tag
	 * @param sValue
	 * @return
	 */
	private String fixNewLine(String sSource, String tag, String sValue) {
		if (StringUtils.isEmpty(sSource)) {
			return "";

		}
		String source = sSource;
		if (sValue.indexOf("\r\n") > 0) {
			sValue = sValue.replace("\r\n", "|");
			String[] values = sValue.split("|");
			// 判断有多少行
			int line = values.length;
			// 找到标签所在的位置
			source = source.replace("\r\n", "");// 整理xml文件
			int tagIndex = source.indexOf(tag);
			// 查找当前标签所对应的<w:p> </w:p>所在的位置
			String[] subSource = source.split(tag);
			int wp1 = subSource[0].lastIndexOf("<w:p w:rsidR");
			int wp2 = subSource[1].indexOf("</w:p>") + subSource[0].length();
			// 复制<w:p> </w:p>的内容line个到</w:p>之后
			String wp = source.substring(wp1, wp2 - wp1 + 10);

			for (int i = 0; i < values.length; i++) {

				source = source.substring(0, wp2 + 10) + wp
						+ source.substring(wp2 + 10, sSource.length());

			}
			for (int i = 0; i < values.length; i++) {
				source = source.replaceFirst(tag, values[i]);
			}
		} else {
			source = source.replace(tag, sValue);
		}
		return source;
	}
}

